package com.yanqu.road.server.manger;

import com.yanqu.road.entity.servercenter.MatchServerGroup;
import com.yanqu.road.entity.servercenter.ServerInfo;
import com.yanqu.road.logic.bussiness.servercenter.ServerBussiness;
import com.yanqu.road.server.TempMgr;
import com.yanqu.road.server.manger.plugin.ManagerReloadListener;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

/**
 * @author ：zfr
 * @date ：2021/2/3 20:47
 * @description：赛事跨服分组
 */
public class MatchServerGroupMgr extends TempMgr {
    private static final Logger logger = LogManager.getLogger(MatchServerGroupMgr.class.getName());

    private static Map<Integer, Map<Integer, MatchServerGroup>> channelServerGroupMap;

    private static Map<Integer, MatchServerGroup> serverGroupMap; // groupId-group

    private static ManagerReloadListener managerReloadListener;

    @Override
    public boolean reload() throws Exception {
        reloadServerGroup();
        return true;
    }

    @Override
    public boolean reloadConfig() throws Exception {
        return true;
    }

    @Override
    public boolean reloadData() throws Exception {
        return true;
    }

    public static void reloadServerGroup() {
        Map<Integer, Map<Integer, MatchServerGroup>> tempChannelServerGroupMap = new ConcurrentHashMap<>();
        Map<Integer, MatchServerGroup> tempServerGroupMap = new ConcurrentHashMap<>();
        ServerBussiness.getMatchServerGroupMap(tempChannelServerGroupMap, tempServerGroupMap);
        channelServerGroupMap = tempChannelServerGroupMap;
        serverGroupMap = tempServerGroupMap;
        notifyReload();
    }

    @Override
    public boolean init() throws Exception {
        return reload();
    }

    @Override
    public boolean stop() {
        return false;
    }

    public static Map<Integer, Map<Integer, MatchServerGroup>> getChannelServerGroupMap() {
        return channelServerGroupMap;
    }

    public static Map<Integer, MatchServerGroup> getServerGroupMap() {
        return serverGroupMap;
    }

    public static Map<Integer, MatchServerGroup> getServerGroupMap(int channelId) {
        return channelServerGroupMap.get(channelId);
    }

    private static void notifyReload(){
        if(managerReloadListener != null){
            managerReloadListener.notifyReload();
        }
    }

    public static void addReloadListener(ManagerReloadListener reloadListener){
        managerReloadListener = reloadListener;
    }

    /**
     * 获取开启赛事的区服分组
     * @param channelId
     * @return
     */
    public static List<Long> getOpenGroupIdList(int channelId) {
        List<Long> openGroupIdList = new ArrayList<>();
        List<ServerInfo> serverInfoList = ServerBussiness.getServerInfoList();
        for (ServerInfo serverInfo : serverInfoList) {
            if (serverInfo.getChannelId() == channelId) {
                openGroupIdList.add(serverInfo.getServerId());
            }
        }
//        Map<Integer, MatchServerGroup> groupMap = channelServerGroupMap.get(channelId);
//        if (groupMap != null) {
//            for (MatchServerGroup group : groupMap.values()) {
//                if (group.getMatchState() == 1) {
//                    openGroupIdList.add(group.getGroupId());
//                }
//            }
//        }
        return openGroupIdList;
    }

    public static MatchServerGroup getServerGroupByServerId(long serverId){
        MatchServerGroup serverGroup = null;
        ServerInfo serverInfo = ServerListMgr.getServerInfo(serverId);
        if(null == serverInfo){
            for(MatchServerGroup tempGroup : serverGroupMap.values()){
                if(tempGroup.getServerList().contains(serverId)){
                    serverGroup = tempGroup;
                    break;
                }
            }
        }else {
            Map<Integer, MatchServerGroup> dataMap = getServerGroupMap(serverInfo.getChannelId());
            if(null != dataMap){
                for(MatchServerGroup tempGroup : dataMap.values()){
                    if(tempGroup.getServerList().contains(serverId)){
                        serverGroup = tempGroup;
                        break;
                    }
                }
            }
        }
        return serverGroup;
    }
}
